﻿using System;
using System.Text;
using System.Collections.Generic;


using DirectUI.Mobile.UI.Controls;
using NMock2;
using Xunit;
//using Rhino.Mocks;
namespace DirectUIUnitTest
{
    /// <summary>
    /// Summary description for PageCollectionDic
    /// </summary>
 
    public class PageCollectionTest
    {
     
        [Fact]
        public void Constuctor_With_Null_Container_Should_Throws()
        {
            ExceptionHelper.ExpectArgumentNullException(
        delegate
        {
             new DirectUI.Mobile.UI.Controls.PageCollectionDic(null);
        },
        "container");
        }
        private Mockery mocks;
        private IContainer container;
        public PageCollectionTest()
        {
         mocks = new Mockery();
          container = (IContainer)mocks.NewMock(typeof(IContainer));
        }

 

 
    [Fact]
        public void Add_Null_Page_Should_Throws()
        {
         ///   Mock<DirectUI.Mobile.UI.Controls.IContainer> container=new Mock<DirectUI.Mobile.UI.Controls.IContainer>();
  
         Stub.On(container).GetProperty("ScaleFactor").Will(Return.Value(1));
        PageCollectionDic pc=new PageCollectionDic(container);
        
        ExceptionHelper.ExpectArgumentNullException(delegate { pc.Add(null); }, "item");
        mocks.VerifyAllExpectationsHaveBeenMet();
    }
    [Fact]
    public void Add_Duplicate_Page_Shoudl_Throws()
    {
        Stub.On(container).GetProperty("ScaleFactor").Will(Return.Value(new System.Drawing.SizeF() { Height = 100, Width = 100 }));
        PageCollectionDic pc = new PageCollectionDic(container);

        IPage page = (IPage)mocks.NewMock(typeof(IPage));
        //最多调用一次Scale函数
        Stub.On(page).Method("Scale").WithAnyArguments();
        Stub.On(page).GetProperty("Name").Will(Return.Value("Test"));
        Stub.On(page).GetProperty("Container");
        Stub.On(page).SetProperty("Container").To(container);
        pc.Add(page);
        
        
        IPage page1 = (IPage)mocks.NewMock(typeof(IPage));
        
        Expect.Never.On(page1).Method("Scale").WithAnyArguments();
        Stub.On(page1).GetProperty("Name").Will(Return.Value("Test"));
        Stub.On(page1).GetProperty("Container");
        Expect.Never.On(page1).SetProperty("Container").To(container);
        //pc.Add(page1);
      ExceptionHelper.ExpectArgumentException(delegate { pc.Add(page1); }, "item");

        //   Assert.IsNotNull(pc["Test"]);
    //    Assert.NotNull(pc["Test"]);
        mocks.VerifyAllExpectationsHaveBeenMet();
    }
    [Fact]
    public void Add_Page_Should_Set_Container_And_Call_Scale_Method_Once()
    {

        Stub.On(container).GetProperty("ScaleFactor").Will(Return.Value(new System.Drawing.SizeF() { Height = 100, Width = 100 }));
        PageCollectionDic pc = new PageCollectionDic(container);
        IPage page1 = (IPage)mocks.NewMock(typeof(IPage));
        //最多调用一次Scale函数
        Expect.Once.On(page1).Method("Scale").WithAnyArguments();
        Stub.On(page1).GetProperty("Name").Will(Return.Value("Test"));
        Stub.On(page1).GetProperty("Container");
        Expect.Once.On(page1).SetProperty("Container").To(container);
        pc.Add(page1);
     //   Assert.IsNotNull(pc["Test"]);
        Assert.NotNull(pc["Test"]);
        mocks.VerifyAllExpectationsHaveBeenMet();
    }
    private PageCollectionDic GetPageCollectionWithTwoPageTest1AndTest2()
    {
        Stub.On(container).GetProperty("ScaleFactor").Will(Return.Value(new System.Drawing.SizeF() { Height = 100, Width = 100 }));
        PageCollectionDic pc = new PageCollectionDic(container);

        IPage page = (IPage)mocks.NewMock(typeof(IPage));
        Expect.Once.On(page).Method("Scale").WithAnyArguments();
        Stub.On(page).GetProperty("Name").Will(Return.Value("Test1"));
        Stub.On(page).GetProperty("Container");
        Stub.On(page).SetProperty("Container").To(container);
        pc.Add(page);


        IPage page1 = (IPage)mocks.NewMock(typeof(IPage));

        Stub.On(page1).Method("Scale").WithAnyArguments();
        Stub.On(page1).GetProperty("Name").Will(Return.Value("Test2"));
        Stub.On(page1).GetProperty("Container");
        Stub.On(page1).SetProperty("Container").To(container);
        pc.Add(page1);
        mocks.VerifyAllExpectationsHaveBeenMet();
        return pc;
    }
    [Fact]
    public void Add_Page_Should_Fire_ControlAdded_Event_And_NotifyChanged_Event()
    {
        Stub.On(container).GetProperty("ScaleFactor").Will(Return.Value(new System.Drawing.SizeF() { Height = 100, Width = 100 }));
        PageCollectionDic pc = new PageCollectionDic(container);
        IPage page1 = (IPage)mocks.NewMock(typeof(IPage));
        //最多调用一次Scale函数
        Stub.On(page1).Method("Scale").WithAnyArguments();
        Stub.On(page1).GetProperty("Name").Will(Return.Value("Test"));
        Stub.On(page1).GetProperty("Container");
        Stub.On(page1).SetProperty("Container");
        IPage addedEvengArgsParm = null;
        bool notifyChanged = false;
        pc.ControlAdded += new EventHandler<ControlEventArgs>(
                   delegate(object sender, ControlEventArgs e)
                   {
                       addedEvengArgsParm = (IPage)e.Control;
                   });
        pc.NotifyChanged += new EventHandler(
            delegate(object sender, EventArgs e)
            {
                notifyChanged = true;
            }
      );
        pc.Add(page1);
  //      Assert.NotNull(pc["Test"]);
        Assert.NotNull(addedEvengArgsParm);
        Assert.Equal<IPage>(page1, addedEvengArgsParm);
        Assert.True(notifyChanged);
        mocks.VerifyAllExpectationsHaveBeenMet();
    }
    [Fact]
    public void Remove_Page_Should_Work_Well_And_Fire_NotifyChanged_Event()
    {
        Stub.On(container).GetProperty("ScaleFactor").Will(Return.Value(new System.Drawing.SizeF() { Height = 100, Width = 100 }));
        PageCollectionDic pc = new PageCollectionDic(container);
        IPage page1 = (IPage)mocks.NewMock(typeof(IPage));
        //最多调用一次Scale函数
        Stub.On(page1).Method("Scale").WithAnyArguments();
        Stub.On(page1).GetProperty("Name").Will(Return.Value("Test"));
        Stub.On(page1).GetProperty("Container");
        Stub.On(page1).SetProperty("Container");
        bool notifyChanged = false;

        pc.NotifyChanged += new EventHandler(
            delegate(object sender, EventArgs e)
            {
                notifyChanged = true;
            }
      );
        pc.Add(page1);
        pc.Remove("Test");
        Assert.False(pc.Contains("Test"));
        Assert.True(notifyChanged);
        mocks.VerifyAllExpectationsHaveBeenMet();
    }
    [Fact]
    public void Remove_Page_Should_Set_Container_To_Null_And_Call_Scale_Method_Once()
    {
        PageCollectionDic pc = GetPageCollectionWithTwoPageTest1AndTest2();
        IPage page1 = pc["Test1"];
       // bool called = false;
        Expect.Once.On(page1).Method("Scale").WithAnyArguments();
     //   Stub.On(page1).GetProperty("Name").Will(Return.Value("Test1"));
      //  Stub.On(page1).GetProperty("Container");
        Expect.Once.On(page1).SetProperty("Container").To(null);
        Assert.True(pc.Remove("Test1"));
        mocks.VerifyAllExpectationsHaveBeenMet();
    }
 
    [Fact]
    public void Contain_Should_Work_Well_IF_With_NullOrEmpty_Argument_Must_Throw()
    {
        PageCollectionDic pc = GetPageCollectionWithTwoPageTest1AndTest2();
        Assert.True(pc.Contains("Test1"));
        Assert.True(pc.Contains(pc["Test1"]));
        ExceptionHelper.ExpectArgumentException(delegate(){pc.Contains("");},"Item");
 //   ExceptionHelper.ExpectArgumentException(delegate(){pc.Contains(null);},"Item");
    }
    }
}
